#include "System.hpp"
using namespace System;

// 菜单函数
void MainMenu();//主菜单
void StudentMenu(Student& student, const vector<Course>& courses, vector<Teacher>& teachers);
void TeacherMenu(Teacher& teacher, const vector<Course>& courses);
void DeanMenu(vector<Course>& courses, vector<Teacher>& teachers, vector<Student>& students);
//Dean是主任

// 全局数据,整个文件都能访问使用，自己添加
vector<Course> courses = {
    Course("数据结构", "计算机科学与技术", 4, 50, 3, "2025-6-10"),
    Course("算法分析", "计算机科学与技术", 3, 45, 2, "2024-6-10"),
    Course("操作系统", "计算机科学与技术", 4, 55, 2, "2025-6-25"),
    Course("高等数学", "数学", 5, 100, 4, "2025-7-05"),
    Course("大学英语", "外语", 3, 60, 5, "2025-7-08"),
    Course("形势与政策", "数学", 5, 100, 4, "2025-6-05"),
    Course("画法几何与机械制图", "数学", 3, 60, 5, "2025-8-08"),
     Course("高等物理", "物理", 5, 100, 4, "2025-6-15"),
    Course("大学化学", "化学", 3, 60, 5, "2025-6-28")
};

//存放所有老师和学生的容器
vector<Teacher> teachers;
vector<Student> students;

int main() {
    // 初始化一些教师(有几个加几个，可连接远程数据库)
    teachers.push_back(Teacher(35, "张老师" ));
    teachers.push_back(Teacher( 40, "李教授" ));
    teachers.push_back(Teacher( 38, "王教授" ));

    // 初始化一些学生(有几个加几个，可连接远程数据库)
    students.push_back(Student{ 20, "周管一" });
    students.push_back(Student{ 21, "辛林东" });
    students.push_back(Student{ 19, "孙东鹏" });

    MainMenu();
    return 0;
}

void MainMenu() {
    int choice;
    do {
        cout << "\n===== 学生选课管理系统 =====" << endl;
        cout << "1. 学生" << endl;
        cout << "2. 教师" << endl;
        cout << "3. 教务处主任" << endl;
        cout << "0. 退出系统" << endl;
        cout << "请选择身份: ";
        cin >> choice;

        switch (choice) {
        case 1: {
            string name;
            cout << "请输入学生姓名: ";
            cin >> name;

            // 查找学生
            auto it = find_if(students.begin(), students.end(),
                [&](const Student& s) { return s._name == name; });

            if (it != students.end()) {
                StudentMenu(*it, courses, teachers);
            }
            else {
                cout << "未找到该学生!" << endl;
                students.push_back(Student{ 0, name });
                StudentMenu(students.back(), courses, teachers);
            }
            break;
        }
        case 2: {
            string name;
            cout << "请输入教师姓名: ";
            cin >> name;

            // 查找教师
            auto it = find_if(teachers.begin(), teachers.end(),
                [&](const Teacher& t) { return t._name == name; });

            if (it != teachers.end()) {
                TeacherMenu(*it, courses);
            }
            else {
                cout << "未找到该教师!" << endl;
                teachers.push_back(Teacher{ 0, name });
                TeacherMenu(teachers.back(), courses);
            }
            break;
        }
        case 3:
            DeanMenu(courses, teachers, students);
            break;
        case 0:
            cout << "系统已退出" << endl;
            break;
        default:
            cout << "无效选择!" << endl;
        }
    } while (choice != 0);
}

void StudentMenu(Student& student, const vector<Course>& courses, vector<Teacher>& teachers) {
    int choice;
    do {
        cout << "\n===== 学生系统 (" << student._name << ") =====" << endl;
        cout << "1. 查看备选课程" << endl;
        cout << "2. 查看教师信息" << endl;
        cout << "3. 选择课程并投票" << endl;
        cout << "4. 删除误选课程" << endl;
        cout << "5. 查看已选课程" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: {
            cout << "\n所有课程信息:" << endl;
            for (const auto& course : courses) {
                course.Display();
            }
            break;
        }
        case 2: {
            cout << "\n教师信息:" << endl;
            for (const auto& teacher : teachers) {
                cout << "姓名: " << teacher._name
                    << " | 年龄: " << teacher._age
                    << " | 教授课程数: " << teacher.selectedCourses.size()
                    << endl;
            }
            break;
        }
        case 3: {
            string courseName;
            cout << "输入要选择的课程名: ";
            cin >> courseName;

            // 检查是否已选
            if (student.HasSelectedCourse(courseName)) {
                cout << "您已选择该课程!" << endl;
                break;
            }

            // 获取课程对应的教师
            vector<string> availableTeachers;
            for (const auto& teacher : teachers) {
                for (const auto& subject : teacher.teachingSubjects) {
                    for (const auto& course : courses) {
                        if (course._CourseName == courseName &&
                            course._Subject == subject) {
                            availableTeachers.push_back(teacher._name);
                            break;
                        }
                    }
                }
            }

            if (availableTeachers.empty()) {
                cout << "该课程暂无候选教师!" << endl;
                break;
            }

            cout << "候选教师: ";
            for (const auto& t : availableTeachers) {
                cout << t << " ";
            }
            cout << endl;

            vector<string> votes;
            cout << "请选择1-2位候选教师(输入姓名，空格分隔): ";
            string t1, t2;
            cin >> t1;
            if (cin.peek() != '\n') {
                cin >> t2;
                votes = { t1, t2 };
            }
            else {
                votes = { t1 };
            }

            // 添加课程
            if (student.AddCourse(courseName, votes)) {
                cout << "选课成功!" << endl;

                // 更新教师票数
                for (auto& teacher : teachers) {
                    if (find(votes.begin(), votes.end(), teacher._name) != votes.end()) {
                        // 这里简化处理，实际应关联具体课程
                    }
                }
            }
            break;
        }
        case 4: {
            string courseName;
            cout << "输入要删除的课程名: ";
            cin >> courseName;
            student.RemoveCourse(courseName);
            break;
        }
        case 5: {
            student.DisplaySelectedCourses();
            break;
        }
        case 0:
            break;
        default:
            cout << "无效选择!" << endl;
        }
    } while (choice != 0);
}

void TeacherMenu(Teacher& teacher, const vector<Course>& courses) {
    int choice;
    do {
        cout << "\n===== 教师系统 (" << teacher._name << ") =====" << endl;
        cout << "1. 查看可教授课程" << endl;
        cout << "2. 选择教授课程" << endl;
        cout << "3. 查看已选课程" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1:
            teacher.ShowAvailableCourses(courses);
            break;
        case 2: {
            string courseName;
            cout << "输入要教授的课程名: ";
            cin >> courseName;

            // 检查是否可教授该课程
            bool canTeach = false;
            for (const auto& subject : teacher.teachingSubjects) {
                for (const auto& course : courses) {
                    if (course._CourseName == courseName &&
                        course._Subject == subject) {
                        canTeach = true;
                        break;
                    }
                }
                if (canTeach) break;
            }

            if (!canTeach) {
                cout << "您不可教授该课程!" << endl;
                break;
            }

            // 添加课程
            if (teacher.AddCourse(courseName)) {
                cout << "课程添加成功!" << endl;
            }
            break;
        }
        case 3:
            teacher.DisplaySelectedCourses();
            break;
        case 0:
            break;
        default:
            cout << "无效选择!" << endl;
        }
    } while (choice != 0);
}

void DeanMenu(vector<Course>& courses, vector<Teacher>& teachers, vector<Student>& students) {
    int choice;
    do {
        cout << "\n===== 教务处管理系统 =====" << endl;
        cout << "1. 管理课程" << endl;
        cout << "2. 查看课程选修情况" << endl;
        cout << "3. 查看教师授课情况" << endl;
        cout << "4. 设置教师可教授课程" << endl;
        cout << "5. 查看教师得票情况" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: {
            int subChoice;
            cout << "\n1. 添加课程" << endl;
            cout << "2. 查看所有课程" << endl;
            cout << "请选择: ";
            cin >> subChoice;

            if (subChoice == 1) {
                Dean::AddCourse(courses);
            }
            else if (subChoice == 2) {
                Dean::DisplayCourses(courses);
            }
            break;
        }
        case 2: {
            cout << "\n课程选修情况:" << endl;
            for (const auto& course : courses) {
                int count = 0;
                for (const auto& student : students) {
                    if (student.HasSelectedCourse(course._CourseName)) {
                        count++;
                    }
                }
                cout << course._CourseName << ": " << count << "名学生选修" << endl;
            }
            break;
        }
        case 3: {
            cout << "\n教师授课情况:" << endl;
            for (const auto& teacher : teachers) {
                cout << "教师: " << teacher._name << endl;
                cout << "可教授学科: ";
                for (const auto& subject : teacher.teachingSubjects) {
                    cout << subject << " ";
                }
                cout << "\n已选课程: ";
                for (const auto& course : teacher.selectedCourses) {
                    cout << course << " ";
                }
                cout << "\n" << endl;
            }
            break;
        }
        case 4: {
            string teacherName;
            cout << "输入教师姓名: ";
            cin >> teacherName;

            auto it = find_if(teachers.begin(), teachers.end(),
                [&](const Teacher& t) { return t._name == teacherName; });

            if (it != teachers.end()) {
                Dean::SetTeacherSubject(*it);
            }
            else {
                cout << "未找到该教师!" << endl;
            }
            break;
        }
        case 5: {
            cout << "\n教师得票情况(简化版):" << endl;
            for (const auto& teacher : teachers) {
                int votes = 0;
                for (const auto& student : students) {
                    for (const auto& record : student.selectedCourses) {
                        for (const auto& vote : record.teacherVotes) {
                            if (vote == teacher._name) {
                                votes++;
                            }
                        }
                    }
                }
                cout << teacher._name << ": " << votes << "票" << endl;
            }
            break;
        }
        case 0:
            break;
        default:
            cout << "无效选择!" << endl;
        }
    } while (choice != 0);
}